<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

body {
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;
  padding: 20px;
  color: #2c3e50;
}

.container {
  max-width: 1400px;
  margin: 0 auto;
}

header {
  text-align: center;
  padding: 30px 0;
  margin-bottom: 30px;
}

header h1 {
  font-size: 2.5rem;
  margin-bottom: 10px;
  color: #3498db;
  text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.1);
}

header p {
  font-size: 1.2rem;
  color: #7f8c8d;
  max-width: 800px;
  margin: 0 auto;
  line-height: 1.6;
}

.app-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 30px;
}

@media (max-width: 1100px) {
  .app-container {
    grid-template-columns: 1fr;
  }
}

.config-panel,
.preview-panel {
  background: white;
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.panel-header {
  background: linear-gradient(135deg, #3498db 0%, #2c3e50 100%);
  color: white;
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 15px;
}

.panel-header i {
  font-size: 1.5rem;
}

.panel-header h2 {
  font-size: 1.8rem;
  font-weight: 500;
}

.panel-content {
  padding: 25px;
  max-height: 600px;
  overflow-y: auto;
}

.config-section {
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #eee;
}

.config-section:last-child {
  border-bottom: none;
  margin-bottom: 0;
  padding-bottom: 0;
}

.config-section h3 {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
  color: #3498db;
  font-size: 1.4rem;
}

.config-group {
  margin-bottom: 20px;
}

.config-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  font-size: 1.1rem;
}

input,
select,
textarea {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 1rem;
  transition: all 0.3s;
}

input:focus,
select:focus,
textarea:focus {
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
  outline: none;
}

.checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  margin-top: 10px;
}

.checkbox-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.checkbox-item input {
  width: auto;
}

.color-picker {
  display: flex;
  gap: 15px;
  align-items: center;
}

.color-preview {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  border: 1px solid #ddd;
}

.actions {
  display: flex;
  gap: 15px;
  margin-top: 30px;
}

.btn {
  padding: 14px 25px;
  border: none;
  border-radius: 8px;
  font-size: 1.1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  gap: 10px;
}

.btn-primary {
  background: linear-gradient(135deg, #3498db 0%, #2c3e50 100%);
  color: white;
  flex: 1;
}

.btn-primary:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(52, 152, 219, 0.4);
}

.btn-secondary {
  background: #ecf0f1;
  color: #2c3e50;
  flex: 1;
}

.btn-secondary:hover {
  background: #d5dbdb;
}

.json-output {
  background: #2c3e50;
  color: #ecf0f1;
  border-radius: 8px;
  padding: 20px;
  font-family: monospace;
  white-space: pre-wrap;
  max-height: 500px;
  overflow-y: auto;
  line-height: 1.6;
  font-size: 0.9rem;
}

.chart-container {
  height: 400px;
  width: 100%;
  margin-top: 20px;
}

.config-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 15px;
}

.notification {
  position: fixed;
  top: 20px;
  right: 20px;
  background: #2ecc71;
  color: white;
  padding: 15px 25px;
  border-radius: 8px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  gap: 10px;
  z-index: 100;
  transform: translateX(150%);
  transition: transform 0.3s ease-out;
}

.notification.show {
  transform: translateX(0);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 15px;
}

.data-table th {
  background: #f8f9fa;
  text-align: left;
  padding: 12px 15px;
  border-bottom: 2px solid #eee;
}

.data-table td {
  padding: 10px 15px;
  border-bottom: 1px solid #eee;
}

.data-table input {
  padding: 8px 12px;
}

.btn-add {
  background: #27ae60;
  color: white;
  border: none;
  padding: 8px 15px;
  border-radius: 6px;
  cursor: pointer;
  margin-top: 10px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.btn-remove {
  background: #e74c3c;
  color: white;
  border: none;
  border-radius: 4px;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.tabs {
  display: flex;
  border-bottom: 1px solid #ddd;
  margin-bottom: 20px;
}

.tab {
  padding: 12px 25px;
  cursor: pointer;
  border-bottom: 3px solid transparent;
  transition: all 0.3s;
}

.tab.active {
  border-bottom: 3px solid #3498db;
  color: #3498db;
  font-weight: 500;
}

.tab-content {
  display: none;
}

.tab-content.active {
  display: block;
}
</style>

<template>
  <div id="app">
    <div class="container">
      <header>
        <h1><i class="fas fa-chart-bar"></i> Vue3 ECharts 配置生成器</h1>
        <p>配置您的ECharts图表选项，实时预览效果并生成可复用的JSON配置文件</p>
      </header>

      <div class="app-container">
        <!-- 配置面板 -->
        <div class="config-panel">
          <div class="panel-header">
            <i class="fas fa-sliders-h"></i>
            <h2>图表配置</h2>
          </div>
          <div class="panel-content">
            <div class="tabs">
              <div class="tab active" @click="activeTab = 'basic'">基本配置</div>
              <div class="tab" @click="activeTab = 'data'" :class="{ active: activeTab === 'data' }">数据配置</div>
              <div class="tab" @click="activeTab = 'style'" :class="{ active: activeTab === 'style' }">样式配置</div>
            </div>

            <!-- 基本配置 -->
            <div class="tab-content active" v-if="activeTab === 'basic'">
              <div class="config-section">
                <h3><i class="fas fa-info-circle"></i> 图表信息</h3>
                <div class="config-group">
                  <label for="chartTitle">图表标题</label>
                  <input type="text" id="chartTitle" v-model="config.title.text" placeholder="销售数据统计">
                </div>

                <div class="config-row">
                  <div class="config-group">
                    <label for="chartType">图表类型</label>
                    <select id="chartType" v-model="config.series[0].type">
                      <option value="bar">柱状图</option>
                      <option value="line">折线图</option>
                      <option value="pie">饼图</option>
                      <option value="scatter">散点图</option>
                      <option value="radar">雷达图</option>
                    </select>
                  </div>

                  <div class="config-group">
                    <label for="theme">主题</label>
                    <select id="theme" v-model="config.theme">
                      <option value="light">浅色主题</option>
                      <option value="dark">深色主题</option>
                      <option value="vintage">复古</option>
                      <option value="macarons">马卡龙</option>
                    </select>
                  </div>
                </div>
              </div>

              <div class="config-section">
                <h3><i class="fas fa-list"></i> 图例设置</h3>
                <div class="config-group">
                  <label>显示图例</label>
                  <div class="checkbox-item">
                    <input type="checkbox" id="showLegend" v-model="config.legend.show">
                    <label for="showLegend">显示图例</label>
                  </div>
                </div>

                <div class="config-row" v-if="config.legend.show">
                  <div class="config-group">
                    <label for="legendPosition">位置</label>
                    <select id="legendPosition" v-model="config.legend.orient">
                      <option value="horizontal">水平</option>
                      <option value="vertical">垂直</option>
                    </select>
                  </div>

                  <div class="config-group">
                    <label for="legendAlign">对齐</label>
                    <select id="legendAlign" v-model="config.legend.align">
                      <option value="left">左对齐</option>
                      <option value="center">居中</option>
                      <option value="right">右对齐</option>
                    </select>
                  </div>
                </div>
              </div>

              <div class="config-section">
                <h3><i class="fas fa-ruler-combined"></i> 坐标轴设置</h3>
                <div class="config-group">
                  <label for="xAxisName">X轴名称</label>
                  <input type="text" id="xAxisName" v-model="config.xAxis.name" placeholder="月份">
                </div>

                <div class="config-group">
                  <label for="yAxisName">Y轴名称</label>
                  <input type="text" id="yAxisName" v-model="config.yAxis.name" placeholder="销售额">
                </div>

                <div class="config-group">
                  <label>显示网格线</label>
                  <div class="checkbox-item">
                    <input type="checkbox" id="showGrid" v-model="config.grid.show">
                    <label for="showGrid">显示网格线</label>
                  </div>
                </div>
              </div>
            </div>

            <!-- 数据配置 -->
            <div class="tab-content" :class="{ active: activeTab === 'data' }">
              <div class="config-section">
                <h3><i class="fas fa-table"></i> 数据系列</h3>
                <div class="config-group">
                  <label>系列名称</label>
                  <input type="text" v-model="config.series[0].name" placeholder="销售数据">
                </div>

                <div class="config-group">
                  <label>X轴数据</label>
                  <div class="data-table-container">
                    <table class="data-table">
                      <thead>
                        <tr>
                          <th>类别</th>
                          <th>操作</th>
                        </tr>
                      </thead>
                      <tbody>
                        <tr v-for="(item, index) in config.xAxis.data" :key="index">
                          <td>
                            <input type="text" v-model="config.xAxis.data[index]">
                          </td>
                          <td>
                            <button class="btn-remove" @click="removeXAxisItem(index)">
                              <i class="fas fa-trash"></i>
                            </button>
                          </td>
                        </tr>
                      </tbody>
                    </table>
                    <button class="btn-add" @click="addXAxisItem">
                      <i class="fas fa-plus"></i> 添加类别
                    </button>
                  </div>
                </div>

                <div class="config-group">
                  <label>Y轴数据</label>
                  <div class="data-table-container">
                    <table class="data-table">
                      <thead>
                        <tr>
                          <th>数值</th>
                          <th>操作</th>
                        </tr>
                      </thead>
                      <tbody>
                        <tr v-for="(item, index) in config.series[0].data" :key="index">
                          <td>
                            <input type="number" v-model.number="config.series[0].data[index]">
                          </td>
                          <td>
                            <button class="btn-remove" @click="removeYAxisItem(index)">
                              <i class="fas fa-trash"></i>
                            </button>
                          </td>
                        </tr>
                      </tbody>
                    </table>
                    <button class="btn-add" @click="addYAxisItem">
                      <i class="fas fa-plus"></i> 添加数据
                    </button>
                  </div>
                </div>
              </div>
            </div>

            <!-- 样式配置 -->
            <div class="tab-content" :class="{ active: activeTab === 'style' }">
              <div class="config-section">
                <h3><i class="fas fa-palette"></i> 颜色设置</h3>
                <div class="config-group">
                  <label>背景颜色</label>
                  <div class="color-picker">
                    <div class="color-preview" :style="{ backgroundColor: config.backgroundColor }"></div>
                    <input type="text" v-model="config.backgroundColor" placeholder="#ffffff">
                  </div>
                </div>

                <div class="config-group">
                  <label>标题颜色</label>
                  <div class="color-picker">
                    <div class="color-preview" :style="{ backgroundColor: config.title.textStyle.color }"></div>
                    <input type="text" v-model="config.title.textStyle.color" placeholder="#333333">
                  </div>
                </div>

                <div class="config-group">
                  <label>系列颜色</label>
                  <div class="color-picker">
                    <div class="color-preview" :style="{ backgroundColor: config.series[0].itemStyle.color }"></div>
                    <input type="text" v-model="config.series[0].itemStyle.color" placeholder="#3498db">
                  </div>
                </div>
              </div>

              <div class="config-section">
                <h3><i class="fas fa-sliders-h"></i> 样式选项</h3>
                <div class="config-row">
                  <div class="config-group">
                    <label for="fontSize">字体大小</label>
                    <input type="number" id="fontSize" v-model.number="config.textStyle.fontSize" min="10" max="24">
                  </div>

                  <div class="config-group">
                    <label for="borderRadius">圆角大小</label>
                    <input type="number" id="borderRadius" v-model.number="config.series[0].itemStyle.borderRadius"
                      min="0" max="20">
                  </div>
                </div>

                <div class="config-group">
                  <label>显示数据标签</label>
                  <div class="checkbox-item">
                    <input type="checkbox" id="showLabel" v-model="config.series[0].label.show">
                    <label for="showLabel">显示数据标签</label>
                  </div>
                </div>

                <div class="config-group" v-if="config.series[0].label.show">
                  <label for="labelPosition">标签位置</label>
                  <select id="labelPosition" v-model="config.series[0].label.position">
                    <option value="top">顶部</option>
                    <option value="bottom">底部</option>
                    <option value="inside">内部</option>
                    <option value="left">左侧</option>
                    <option value="right">右侧</option>
                  </select>
                </div>
              </div>
            </div>

            <div class="actions">
              <button class="btn btn-primary" @click="generateConfig">
                <i class="fas fa-file-code"></i> 生成配置
              </button>
              <button class="btn btn-secondary" @click="resetConfig">
                <i class="fas fa-redo"></i> 重置配置
              </button>
            </div>
          </div>
        </div>

        <!-- 预览面板 -->
        <div class="preview-panel">
          <div class="panel-header">
            <i class="fas fa-eye"></i>
            <h2>预览 & 输出</h2>
          </div>
          <div class="panel-content">
            <h3>图表预览</h3>
            <div class="chart-container" ref="chartDom"></div>

            <h3 style="margin: 25px 0 15px;">配置JSON</h3>
            <div class="json-output">
              {{ jsonConfig }}
            </div>

            <div class="actions">
              <button class="btn btn-primary" @click="copyConfig">
                <i class="fas fa-copy"></i> 复制配置
              </button>
              <button class="btn btn-secondary" @click="downloadConfig">
                <i class="fas fa-download"></i> 下载配置
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="notification" :class="{ 'show': showNotification }">
      <i class="fas fa-check-circle"></i>
      {{ notificationMessage }}
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, watch, nextTick } from "vue";
import * as echarts from 'echarts';

// 初始配置
const config = reactive({
  title: {
    text: '销售数据统计',
    textStyle: {
      color: '#333',
      fontSize: 18
    },
    left: 'center'
  },
  legend: {
    show: true,
    orient: 'horizontal',
    align: 'center',
    data: ['销售数据']
  },
  grid: {
    show: true,
    left: '10%',
    right: '10%',
    bottom: '15%',
    containLabel: true
  },
  xAxis: {
    name: '月份',
    type: 'category',
    data: ['1月', '2月', '3月', '4月', '5月', '6月']
  },
  yAxis: {
    name: '销售额',
    type: 'value'
  },
  series: [{
    name: '销售数据',
    type: 'bar',
    data: [150, 230, 224, 218, 135, 147],
    itemStyle: {
      color: '#3498db',
      borderRadius: 5
    },
    label: {
      show: true,
      position: 'top'
    }
  }],
  backgroundColor: '#ffffff',
  textStyle: {
    fontSize: 14
  },
  theme: 'light'
})

const jsonConfig = ref("")
const showNotification = ref(false)
const notificationMessage = ref("")
const chartDom = ref(null)
const chartInstance = ref(null)
const activeTab = ref("basic")

// 初始化图表
const initChart = () => {
  if (!chartDom.value) return

  // 销毁旧实例
  if (chartInstance.value) {
    chartInstance.value.dispose()
  }

  // 创建新实例
  chartInstance.value = echarts.init(chartDom.value)

  // 应用配置
  chartInstance.value.setOption(config)

  // 响应窗口变化
  window.addEventListener('resize', () => {
    chartInstance.value.resize()
  })
}

// 生成配置JSON
const generateConfig = () => {
  // 创建配置对象的副本
  const configCopy = JSON.parse(JSON.stringify(config))

  // 格式化JSON输出
  jsonConfig.value = JSON.stringify(configCopy, null, 2)

  showNotification.value = true
  notificationMessage.value = "配置已生成!"

  setTimeout(() => {
    showNotification.value = false
  }, 3000)
}

// 复制配置到剪贴板
const copyConfig = () => {
  navigator.clipboard.writeText(jsonConfig.value)
    .then(() => {
      notificationMessage.value = "配置已复制到剪贴板!"
      showNotification.value = true

      setTimeout(() => {
        showNotification.value = false
      }, 3000)
    })
    .catch(err => {
      notificationMessage.value = "复制失败: " + err
      showNotification.value = true

      setTimeout(() => {
        showNotification.value = false
      }, 3000)
    })
}

// 下载配置
const downloadConfig = () => {
  const blob = new Blob([jsonConfig.value], { type: 'application/json' })
  const url = URL.createObjectURL(blob)
  const a = document.createElement('a')
  a.href = url
  a.download = 'echarts-config.json'
  document.body.appendChild(a)
  a.click()
  document.body.removeChild(a)
  URL.revokeObjectURL(url)

  notificationMessage.value = "配置已下载!"
  showNotification.value = true

  setTimeout(() => {
    showNotification.value = false
  }, 3000)
}

// 重置配置
const resetConfig = () => {
  Object.assign(config, {
    title: {
      text: '销售数据统计',
      textStyle: {
        color: '#333',
        fontSize: 18
      },
      left: 'center'
    },
    legend: {
      show: true,
      orient: 'horizontal',
      align: 'center',
      data: ['销售数据']
    },
    grid: {
      show: true,
      left: '10%',
      right: '10%',
      bottom: '15%',
      containLabel: true
    },
    xAxis: {
      name: '月份',
      type: 'category',
      data: ['1月', '2月', '3月', '4月', '5月', '6月']
    },
    yAxis: {
      name: '销售额',
      type: 'value'
    },
    series: [{
      name: '销售数据',
      type: 'bar',
      data: [150, 230, 224, 218, 135, 147],
      itemStyle: {
        color: '#3498db',
        borderRadius: 5
      },
      label: {
        show: true,
        position: 'top'
      }
    }],
    backgroundColor: '#ffffff',
    textStyle: {
      fontSize: 14
    },
    theme: 'light'
  })

  jsonConfig.value = ""

  notificationMessage.value = "配置已重置!"
  showNotification.value = true

  setTimeout(() => {
    showNotification.value = false
  }, 3000)
}

// 添加X轴数据
const addXAxisItem = () => {
  config.xAxis.data.push(`类别${config.xAxis.data.length + 1}`)
}

// 移除X轴数据
const removeXAxisItem = (index) => {
  config.xAxis.data.splice(index, 1)
}

// 添加Y轴数据
const addYAxisItem = () => {
  config.series[0].data.push(0)
}

// 移除Y轴数据
const removeYAxisItem = (index) => {
  config.series[0].data.splice(index, 1)
}

// 监听配置变化，更新图表
watch(config, () => {
  if (chartInstance.value) {
    chartInstance.value.setOption(config)
  }
}, { deep: true })

// 初始化
onMounted(() => {
  initChart()
  generateConfig()
})


</script>